<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Send Notification (Google Chat) - The Core
 * @description : Send a chat message to users for notifying new tweets, replies, polls, etc..
 *                # This will accept a CLI request & send chat messages
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Background_process extends CI_Controller {

  /**
   * Checking weather request is from command line
   * 
   */
  public function __construct() {
    parent::__construct();
    if (!$this->input->is_cli_request()) {
      redirect();
    }
  }

  /**
   * Sending chat notifictaion
   * 
   */
  public function send_chat($email_id = NULL, $chat_message = NULL) {

    // Decoding Email id & Chat message
    $email_id = base64_decode($email_id);
    $chat_message = base64_decode($chat_message);

    // Checking email & message
    if (!empty($email_id) && !empty($chat_message)) {

      // Preparing request params
      $params = array(
          'host' => 'talk.google.com',
          'port' => 5222,
          'user' => $this->config->item('smtp_user'),
          'password' => $this->config->item('smtp_pass'),
          'resource' => 'xmpphp',
          'server' => 'gmail.com',
          'printlog' => FALSE,
          'loglevel' => 2
      );

      // Load library using provided params
      $this->load->library('xmpphp/xmpp', $params);

      // Send chat message
      try {
        $this->xmpp->connect();
        $this->xmpp->processUntil('session_start');
        $this->xmpp->presence();
        $this->xmpp->message($email_id, $chat_message);
        //$this->xmpp->disconnect();
      } catch (XMPPHP_Exception $e) {
        die($e->getMessage());
      }
    }

    // Stop execution
    die();
  }

}

?>